﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using GTA;

namespace GTAHookAdv.Screen
{
    /// <summary>
    /// ResolutionHelper reads the game screen resolution at runtime and provide a set of methods to convert a point or region from a 1920x1080 resolution to the actual game resolution.
    /// In your script always set coordinates as if every players have a 1920x1080 resolution, then use this class to make sure that everything will fit the final gamer screen.
    /// </summary>
    /// <remarks>IMPORTANT : if player changes game Resolution, it will take effect only when script will be reloaded (when game restarts, or player load a savedgame).
    /// This is not my fault, it depend on NetScriptHook behavior :(</remarks>
    public class ResolutionHelper
    {
        /// <summary>
        /// see <see cref="ResolutionHelper"/>
        /// </summary>
        public static PointF ConvertToScr(PointF pt)
        {
            PointF ratio = GetResolutionRatio();
            if (ratio.X == 1 && ratio.Y == 1)
                return pt;
            return new PointF(pt.X / ratio.X, pt.Y / ratio.Y);
        }

        /// <summary>
        /// see <see cref="ResolutionHelper"/>
        /// </summary>
        public static int ConvertToScrX(int x)
        {
            PointF ratio = GetResolutionRatio();
            if (ratio.X == 1)
                return x;
            return Convert.ToInt32(((float)x) / ratio.X);
        }

        /// <summary>
        /// see <see cref="ResolutionHelper"/>
        /// </summary>
        public static int ConvertToScrY(int y)
        {
            PointF ratio = GetResolutionRatio();
            if (ratio.Y == 1)
                return y;
            return Convert.ToInt32(((float)y) / ratio.Y);
        }

        /// <summary>
        /// see <see cref="ResolutionHelper"/>
        /// </summary>
        private static PointF GetResolutionRatio()
        {
            return GTA.Graphics.Convert(new PointF(1920, 1080), FontScaling.Pixel, FontScaling.ScreenUnits);
        }

        /// <summary>
        /// Gives the current screen Resolution in game
        /// see VERY IMPORTANT remarks in docs
        /// </summary>
        /// <remarks>IMPORTANT : if player changes game Resolution, it will take effect only when script will be reloaded (at game launch, or when player loads a savedgame).
        /// This is not my fault, it depend on NetScriptHook behavior :(</remarks>
        public static Size CurrentResolution
        {
            get
            {
                PointF ratio = GetResolutionRatio();
                return new Size(Convert.ToInt32(1920f * ratio.X), Convert.ToInt32(1080f * ratio.Y));
            }
        }

        /// <summary>
        /// see <see cref="ResolutionHelper"/>
        /// </summary>
        public static SizeF ConvertToScr(SizeF size)
        {
            PointF ratio = GetResolutionRatio();
            if (ratio.X == 1 && ratio.Y == 1)
                return size;
            return new SizeF(size.Width / ratio.X, size.Height / ratio.Y);
        }

        /// <summary>
        /// see <see cref="ResolutionHelper"/>
        /// </summary>
        public static RectangleF ConvertToScr(RectangleF rect)
        {
            PointF ratio = GetResolutionRatio();
            if (ratio.X == 1 && ratio.Y == 1)
                return rect;
            return new RectangleF(ConvertToScr(rect.Location), ConvertToScr(rect.Size));

        }
    }
}
